    package com.edu.bhu.admin.controller.admin;

import java.util.List;

import com.edu.bhu.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.edu.bhu.common.annotation.Log;
import com.edu.bhu.common.enums.BusinessType;
import com.edu.bhu.admin.domain.CosCart;
import com.edu.bhu.admin.service.ICosCartService;
import com.edu.bhu.common.core.controller.BaseController;
import com.edu.bhu.common.core.domain.AjaxResult;
import com.edu.bhu.common.utils.poi.ExcelUtil;
import com.edu.bhu.common.core.page.TableDataInfo;

/**
 * 购物车Controller
 * 
 * @author wangjinao
 * @date 2022-03-17
 */
@Controller
@RequestMapping("/admin/cart")
public class CosCartController extends BaseController
{
    private String prefix = "admin/cart";

    @Autowired
    private ICosCartService cosCartService;

    @RequiresPermissions("admin:cart:view")
    @GetMapping()
    public String cart()
    {
        return prefix + "/cart";
    }

    /**
     * 查询购物车列表
     */
    @RequiresPermissions("admin:cart:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CosCart cosCart)
    {
        Long userId = ShiroUtils.getSysUser().getUserId();
        cosCart.setUserId(userId);
        startPage();
        List<CosCart> list = cosCartService.selectCosCartList(cosCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @RequiresPermissions("admin:cart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CosCart cosCart)
    {
        List<CosCart> list = cosCartService.selectCosCartList(cosCart);
        ExcelUtil<CosCart> util = new ExcelUtil<CosCart>(CosCart.class);
        return util.exportExcel(list, "购物车数据");
    }

    /**
     * 新增购物车
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存购物车
     */
    @RequiresPermissions("admin:cart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CosCart cosCart)
    {
        cosCart.setUserId(ShiroUtils.getSysUser().getUserId());
        return toAjax(cosCartService.insertCosCart(cosCart));
    }

    /**
     * 修改购物车
     */
    @RequiresPermissions("admin:cart:edit")
    @GetMapping("/edit/{cartId}")
    public String edit(@PathVariable("cartId") Long cartId, ModelMap mmap)
    {
        CosCart cosCart = cosCartService.selectCosCartByCartId(cartId);
        mmap.put("cosCart", cosCart);
        return prefix + "/edit";
    }

    /**
     * 修改保存购物车
     */
    @RequiresPermissions("admin:cart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CosCart cosCart)
    {
        return toAjax(cosCartService.updateCosCart(cosCart));
    }

    /**
     * 删除购物车
     */
    @RequiresPermissions("admin:cart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cosCartService.deleteCosCartByCartIds(ids));
    }
}
